/**
 * @(#)OrderManagementServiceImpl.java 2011
 *
 *
 * Copyright 2011 classic-commerce
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 */

package ca.ioniq.server.service.impl;

import java.util.Date;
import java.util.List;

import javax.inject.Inject;

import org.apache.commons.beanutils.BeanUtils;

import ca.ioniq.server.dao.OrderDAO;
import ca.ioniq.server.service.OrderManagementService;
import ca.ioniq.server.service.exception.OrderException;
import ca.ioniq.shared.entity.Order;
import ca.ioniq.shared.entity.Status;

import com.google.inject.persist.Transactional;

/**
 * TODO the order should be viewed by companyId
 *
 * We need to find a way to tie it up to a companyId somehow
 *
 * @author: <a href="mailto:richard@ioniq.ca">Richard Mutezintare</a>
 */
public class OrderManagementServiceImpl implements OrderManagementService {

	private final OrderDAO orderDao;

	@Inject
	public OrderManagementServiceImpl(OrderDAO orderDao) {
		this.orderDao = orderDao;
	}

	@Transactional
	public List<Order> viewLastOrders(Date date) {
		return orderDao.findOrdersAfterAGivenDate(date);
	}

	public List<Order> viewLastOrdersByCompany(Date date, Long companyId) {
		return orderDao.findOrdersAfterAGivenDateAndCompany(date, companyId);
	}

	@Transactional
	public List<Order> viewAllOrders() {
		return orderDao.findAll();
	}

	public List<Order> viewAllOrdersByCompany(Long companyId) {
		return orderDao.findAllByCompany(companyId);
	}

	@Transactional
	public List<Order> viewOrdersByUser(Long userId) {
		return orderDao.viewOrdersByUser(userId);
	}

	public List<Order> viewOrdersByUserAndCompany(Long userId, Long companyId) {
		return orderDao.viewOrdersByUserAndCompany(userId, companyId);
	}
	
	@Transactional
	public Order viewOrder(long orderId) throws OrderException {
		try {
			return orderDao.findById(orderId);
		} catch (Exception e) {
			throw new OrderException("Maybe the order with this id is not in the repository", e);
		}
	}

	@Transactional
	public boolean cancelOrder(long orderId) {
		Order currentOrder = orderDao.findById(orderId);
		currentOrder.setStatus(Status.CANCELLED.getId());
		orderDao.update(currentOrder);
		return true;
	}

	@Transactional
	public void updateOrder(Order order, long orderId) throws OrderException {
		try {
			Order currentOrder = orderDao.findById(orderId);
			BeanUtils.copyProperties(currentOrder, order);
			currentOrder.setId(orderId);
			orderDao.update(currentOrder);
		} catch (Exception e) {
			throw new OrderException("Cannot update this order; maybe is not in the repository", e);
		}
	}

	@Transactional
	public void updateOrder(Order order) throws OrderException {
		try {
			updateOrder(order, order.getId());
		} catch (Exception e) {
			throw new OrderException("Cannot update this order; maybe is not in the repository", e);
		}
	}

	@Transactional
	public Order getOrder(Long id) {
		return orderDao.findById(id);
	}

	@Transactional
	public Order putOrder(Order order) {
		orderDao.persist(order);
		return order;
	}
}
